package com.ht.medicaltreatment.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.NestedScrollingParent2
import androidx.core.view.ViewCompat
import androidx.core.view.marginBottom
import androidx.core.view.marginTop
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.button.MaterialButton
import com.ht.medicaltreatment.R

class ScrollConstraintLayout @JvmOverloads constructor(context: Context,attrs:AttributeSet?=null,defStyleAttr:Int = -1)
    : ConstraintLayout(context,attrs,defStyleAttr),NestedScrollingParent2{

    private var mTopViewHeight = 0
    private lateinit var mTopView:ImageView
    private lateinit var recyclerView:RecyclerView
    private lateinit var btnNext: MaterialButton
   // private lateinit var name: TextView
    private lateinit var imgTabBg: ImageView
    private lateinit var imgListBg: ImageView

    override fun onStartNestedScroll(child: View, target: View, axes: Int, type: Int): Boolean {
        return axes and ViewCompat.SCROLL_AXIS_VERTICAL != 0
    }

    override fun onNestedScrollAccepted(child: View, target: View, axes: Int, type: Int) {

    }

    override fun onStopNestedScroll(target: View, type: Int) {

    }

    override fun onNestedScroll(
        target: View,
        dxConsumed: Int,
        dyConsumed: Int,
        dxUnconsumed: Int,
        dyUnconsumed: Int,
        type: Int
    ) {

    }

    override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) {
        val hideTop = dy > 0 && scrollY < mTopViewHeight
        val showTop = dy < 0 && scrollY >= 0 && !target.canScrollVertically(-1)
        if(showTop || hideTop){
            scrollBy(0,dy)
            consumed[1] = dy
        }
    }

    private var widthMeasureSpec = 0
    private var heightMeasureSpec = 0
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        this.widthMeasureSpec = widthMeasureSpec
        this.heightMeasureSpec = heightMeasureSpec

        val params = recyclerView.layoutParams
        params.height = measuredHeight - otherHeight
        recyclerView.layoutParams = params
        //LogUtils.i("height2",params.height,mTopViewHeight)
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        mTopView = findViewById(R.id.imgIcon)
        recyclerView = findViewById(R.id.recyclerView)
        //name = findViewById(R.id.tvName)
        imgTabBg = findViewById(R.id.imgTabBg)
        imgListBg = findViewById(R.id.imgListBg)
    }

    var otherHeight = 0

    //减去其他不被隐藏的View的高度
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        val parentView = parent as ViewGroup
        btnNext = parentView.findViewById(R.id.btnNext)
        val title = parentView.findViewById<ViewGroup>(R.id.include)

        mTopViewHeight = mTopView.measuredHeight + mTopView.marginTop
        otherHeight = btnNext.height +
                btnNext.marginTop +
                btnNext.marginBottom+
                /*name.height +
                name.marginTop+
                name.marginBottom+*/
                imgTabBg.height+
                imgTabBg.marginTop+
                imgTabBg.marginBottom+
                title.height
        measure(widthMeasureSpec,heightMeasureSpec)
    }

    override fun scrollTo(x: Int, y: Int) {
        var scrollY = y
        if(scrollY < 0){
            scrollY = 0
        }
        if(scrollY > mTopViewHeight){
            scrollY = mTopViewHeight
        }
        super.scrollTo(x, scrollY)
    }

}